package org.nutz.j2cache.spring;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;

import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;

public class SpringJ2Cache implements Cache {

	protected String region;
	
	protected CacheChannel channel;
	
	public SpringJ2Cache(String region, CacheChannel channel) {
		this.region = region;
		this.channel = channel;
	}

	public String getName() {
		return region;
	}

	public Object getNativeCache() {
		return this.channel;
	}

	public ValueWrapper get(Object key) {
		CacheObject val = this.channel.get(region, key);
		if (val == null || val.getValue() == null)
			return null;
		return new SimpleValueWrapper(val.getValue());
	}

	public void put(Object key, Object value) {
		channel.set(region, key, value);
	}

	public void evict(Object key) {
		channel.evict(region, key);
	}

	public void clear() {
		channel.clear(region);
	}

}
